package kr.hkit.androidcomplete03.ch20;


import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListFragmentEx extends Activity {
	static String WORDS[];
	static String DESC[];
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		WORDS = getResources().getStringArray(R.array.ch20_listfragment_word);
		DESC = getResources().getStringArray(R.array.ch20_listfragment_desc);
		
		FragmentManager FM = getFragmentManager();
		if(FM.findFragmentById(android.R.id.content) == null){
			WordListFragmentEx wordlist = new WordListFragmentEx();
			FM.beginTransaction().add(android.R.id.content, wordlist).commit();
			
		}//end if
	}//end oncreate()
	
	public class WordListFragmentEx extends ListFragment{
		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onActivityCreated(savedInstanceState);
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, WORDS);
			
			setListAdapter(adapter);
		}
		
		@Override
		public void onListItemClick(ListView l, View v, int position, long id) {
			String text = WORDS[position] + " : " +DESC[position];
			Toast.makeText(getActivity(), text, 0).show();
		}
	}
}
